<?
//*********************************直接在这里设置通过表前缀***********************/

include_once('conn_db.php');
CLASS mydb{
	var $conn = null; 
	var $rstotal = 0;
	//////////////////////////构造函数////////////////////////
	function mydb()	{
		$this->connect(HOST,NAME,PASS,DBNAME,CODEPAGE);
	}
	/////////////////////////析构函数//////////////////////
	function __destruct(){
		$this-> close();
	}
	////////////////////////连接数据库////////////////////////
	function connect($host,$name,$pass,$dbname,$codepage){
		$this->conn=mysqli_connect($host,$name,$pass,$dbname) or die($this->error());
		mysqli_query($this->conn,"SET NAMES '".$codepage."'");
		// mysql_select_db($dbname,$this->conn) or die($this->error());
	}
	//////////////////////执行SQL////////////////////////
	function query($sql){//返回结果集
		$query=mysqli_query($this->conn,$sql) or die($this->error());
		$this->rstotal=mysqli_num_rows($query);
		return $query;
	}
	//////////////////////返回行数////////////////////////
	function num_rows($sql){
		$result=mysqli_query($this->conn,$sql) or die($this->error());
		$numrows=mysqli_num_rows($result);
		return $numrows;
	}
	//////////////////////错误函数////////////////////////
	function error(){
		return mysql_error();
	}
	//////////////////////关闭连接////////////////////////
	function close(){
		@mysql_close($this->conn);
	}
	//////////////////////读取一条数据////////////////////
	function fetch_assoc($sql){
		$result=$this->query($sql);
		return mysqli_fetch_assoc($result) ;
	}
	//////////////////////记录集总数//////////////////////
	function rstotal($result){
		return mysqli_num_rows($result);
	}
	//////////////////////读取全部数据//////////////////////
	function fetchAll($sql){
		$rs=array();
		$result=$this->query($sql);
		while($row=mysqli_fetch_assoc($result)){
			$rs[]=$row;
		}
		return $rs;
	}
	//////////////////////分页读取//////////////////////
	function fetchbypage($sql,$pagesize=3,$page=1){
		$result=$this->query($sql);
		$count[]=array();
		$count['rscount']=$this->rstotal;
		$count['pagesize']=(int)$pagesize;
		$count['pagecount']=ceil($count['rscount'] / $pagesize);
		$count['page']=$count['pagecount']== 0 ? 0 : ((int)$page<1 ? 1:(int)$page);
		$count['page']=$count['page']>$count['pagecount'] ? $count['pagecount'] : $count['page'];
		$count['info'].=' 共有记录<b>'.$count['rscount'].'</b>条';
		$count['info'].=' 每页显示<b>'.$pagesize.'</b>条';
		$count['info'].=' 当前页数<b>'.$count['page'].'</b>/'.$count['pagecount'];
		$star=$count['page'] ==0 ? 0: ($count['page']-1)*$count['pagesize'];
		$sql=mysql_escape_string($sql);
		$sql.=' limit ' . $star . ','. $count['pagesize'];
		//echo $sql;
		$count['data']=$this->fetchAll($sql);
		return $count;
	}
}

?>